package com.zpkj.portal.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;

@Component
public class BeanFactoryUtils implements BeanFactoryAware {

	private static BeanFactory beanFactory; // BEAN工厂

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		BeanFactoryUtils.beanFactory = beanFactory;
	}

	public BeanFactory getBeanfactory() {
		return beanFactory;
	}

	/**
	 * 根据提供的bean名称及类型得到相应的服务类
	 * 
	 * @param servName
	 *            bean名称
	 * @param clazz
	 */
	public static <T> T getBean(String servName,Class<T> clazz) {
		return beanFactory.getBean(servName, clazz);
	}
	
	/**
	 * 通过Bean Id获取Bean
	 * @param <T>
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		return (T)beanFactory.getBean(name);
	}
	
	/**
	 * 通过类型获取Bean
	 * @param <T>
	 * @param clazz
	 * @return
	 */
	public static <T> T getBean(Class<T> clazz) {
		return beanFactory.getBean(clazz);
	}

}
